
export default {
    namespaced: true,
    state: {
        cartData:[],
    },
    getters: {
        total(state){
           return state.cartData.reduce((total,item)=>{
               return total += item.price*item.count
            },0)
        }
    },
    mutations: {
        SET(state,payload){
            let target = state.cartData.find(item=>item.id===payload.id
            )
            if (!target) {
                let obj = {
                    title:payload.title,
                    price:payload.price,
                    count:1,
                    id:payload.id
                }
                state.cartData.push(obj)
            }else{
                target.count++
            }
            // state.cartData.push(payload)
        },
        // JIAN(state,payload){
        //     state.cartData
        // }
    },
    
    actions: {
        ADD_CART(context,payload){
            // console.log(payload);
            context.commit('SET',payload)
            context.commit('products/DECREASE_INVENTORY',payload.id,{
                root:true
            })
        },
        CCC(context,payload){
            context.commit('products/DDDJIAJIA',payload.id,{root:true})
        }
    }
}